package com.cwt.admin.index;

import cn.dev33.satoken.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cwt.admin.common.BaseController;
import com.cwt.common.result.AjaxResult;
import com.cwt.common.utils.DateUtils;
import com.cwt.common.utils.SqlUtils;
import com.cwt.common.vo.PageInfo;
import com.cwt.service.domain.OrderItem;
import com.cwt.service.domain.ShopCar;
import com.cwt.service.domain.ShopOrder;
import com.cwt.service.service.OrderItemService;
import com.cwt.service.service.ShopOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author chenwt
 * @version V1.0
 * @describe 【订单相关接口】
 * @date 2023年08月09日 17:15:38
 */
@Api(tags = "订单相关接口", value = "OrderController")
@RequestMapping("/order")
@RestController
@SaCheckOr(
        login = @SaCheckLogin,
        role = @SaCheckRole("manage"),
        permission = @SaCheckPermission("order")
)
public class OrderController extends BaseController {

    @Autowired
    private ShopOrderService orderService;

    @Autowired
    private OrderItemService itemService;

    @ApiOperation(value = "生成支付二维码", protocols = "http", httpMethod = "POST")
    @RequestMapping(value = "/qrcode", method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody AjaxResult qrcode(@RequestBody ShopOrder order) {
        return orderService.qrcode(order);
    }

    @ApiOperation(value = "移除支付二维码", protocols = "http", httpMethod = "POST")
    @RequestMapping(value = "/removeQrcode", method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody AjaxResult removeQrcode(@RequestBody String qrcodeName) {
        return orderService.removeQrcode(qrcodeName);
    }

    @ApiOperation(value = "新增订单", protocols = "http", httpMethod = "POST")
    @RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody AjaxResult add(@RequestBody ShopOrder order) {
        order.getItems().forEach(item -> item.setOrderNumber(order.getOrderNumber()));
        if (itemService.saveBatch(order.getItems())) {
            order.setCreateTime(DateUtils.formatFullDateTime(new Date()));
            return AjaxResult.success(orderService.save(order));
        }
        return AjaxResult.error("订单新增失败");
    }

    @ApiOperation(value = "订单分页", protocols = "http", httpMethod = "POST")
    @RequestMapping(value = "/page", method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody AjaxResult shopCarPage(@RequestBody PageInfo info) {
        Page<ShopOrder> page = new Page<>(info.getPageNum(), info.getPageSize());
        QueryWrapper<ShopOrder> queryWrapper = new QueryWrapper<>();
        SqlUtils.wrapperSetLikeCondition(queryWrapper, info.getParams());
        page = orderService.page(page, queryWrapper);
        page.getRecords().forEach(order -> {
            QueryWrapper<OrderItem> wrapper = new QueryWrapper<>();
            wrapper.eq("order_number", order.getOrderNumber());
            order.setItems(itemService.list(wrapper));
        });
        return AjaxResult.success(page);
    }

    @ApiOperation(value = "获取支付二维码", protocols = "http", httpMethod = "POST")
    @RequestMapping(value = "/getQrcode", method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody AjaxResult getQrcode(@RequestBody String orderNumber) {
        return orderService.getQrcode(orderNumber);
    }

}
